package ch.zhaw.hgc.Utils.Comparators;

import java.util.Comparator;

import ch.zhaw.hgc.Enumerations.RecipeComparableField;
import ch.zhaw.hgc.Enumerations.SortDirection;
import ch.zhaw.hgc.entities.Recipe;

public class RecipeComparator implements Comparator<Recipe> {

	private RecipeComparableField comparableField;
	private SortDirection sortDirection;

	@Override
	public int compare(Recipe o1, Recipe o2) {
		int compareResult;
		if (comparableField == RecipeComparableField.Author) {
			compareResult = o1.getAuthor().compareTo(o2.getAuthor());
		} else if (comparableField == RecipeComparableField.Title) {
			compareResult = o1.getTitle().compareTo(o2.getTitle());
		} else {
			compareResult = o1.getTitle().compareTo(o2.getTitle());
		}
		if (this.sortDirection == SortDirection.Descending) {
			compareResult = compareResult * (-1);
		}
		return compareResult;
	}

	/**
	 * @return the comparableField
	 */
	public RecipeComparableField getComparableField() {
		return comparableField;
	}

	/**
	 * @param comparableField
	 *            the comparableField to set
	 */
	public void setComparableField(RecipeComparableField comparableField) {
		this.comparableField = comparableField;
	}

	/**
	 * @return the sortDirection
	 */
	public SortDirection getSortDirection() {
		return sortDirection;
	}

	/**
	 * @param sortDirection
	 *            the sortDirection to set
	 */
	public void setSortDirection(SortDirection sortDirection) {
		this.sortDirection = sortDirection;
	}
}
